
package management;

import entities.Enemy;
import entities.PlayableChar;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import org.newdawn.slick.SlickException;
import physics.BlockMap;

public class ObjectDispatcher {

    DBAccess db = new DBAccess();
    private static HashMap<String, PlayableChar> pnjs = new HashMap<String, PlayableChar>();
    private static HashMap<String, Enemy> enemies = new HashMap<String, Enemy>();
    private static BlockMap bm;
    private static int zone;
    public static int idPartida;



    public BlockMap crearBlockMap(int zone) throws SQLException, SlickException {
        bm = new BlockMap(db.getMapPath(zone));
        return bm;
    }



    public void crearHashMapPnjs() throws SQLException {
        String[] pnjNames = {"Soldier","BountyHunter","Rogue","Wolf"};
        int i = 1;
        for (i = 0; i < pnjNames.length; i++) {
            setPnj(pnjNames[i], crearJugador(pnjNames[i]));
        }
    }

    public PlayableChar crearJugador(String nomPj) throws SQLException {
        PlayableChar pnj = new PlayableChar(db.heroIniValues(nomPj), db.CharAnimations(nomPj));
        return pnj;
    }

  
    //este constructor es diferente para optimizar el acceso a bdd
    public Enemy crearEnemy(ResultSet iniValues, ResultSet animations) throws SQLException {
        Enemy enemy = new Enemy(iniValues, animations);
        return enemy;
    }
    //la key del array de enemigos es el nombre del enemigo + un indice numerico
    public void crearHashMapEnemies(int qTipus1, String nomTipus1, int qTipus2, String nomTipus2, int qTipus3, String nomTipus3) throws SQLException {
        ResultSet iniValues;
        ResultSet animations;
        int i;
        if (qTipus1 > 0 && nomTipus1 != null) {
             for (i = 1; i <= qTipus1; i++) {
            iniValues = db.EnemyIniValues(nomTipus1);
            animations = db.CharAnimations(nomTipus1);
                setEnemy(nomTipus1 + i, crearEnemy(iniValues, animations));
            }
        }
        if (qTipus2 > 0 && nomTipus2 != null) {
            iniValues = db.EnemyIniValues(nomTipus2);
            animations = db.CharAnimations(nomTipus2);
            iniValues.first();
            animations.first();
            for (i = 1; i < qTipus2; i++) {
                setEnemy(nomTipus2 + i, crearEnemy(iniValues, animations));
            }
        }
        if (qTipus3 > 0 && nomTipus3 != null) {
            iniValues = db.EnemyIniValues(nomTipus3);
            animations = db.CharAnimations(nomTipus3);
            iniValues.first();
            animations.first();
            for (i = 1; i < qTipus3; i++) {
                setEnemy(nomTipus3 + i, crearEnemy(iniValues, animations));
            }
        }
    }

    public static HashMap<String, Enemy> getEnemies() {
        return enemies;
    }

    public static void setEnemy(String nom, Enemy enemy) {
        enemies.put(nom, enemy);
    }

    public static int getZone() {
        return zone;
    }

    public static void setZone(int zone) {
        ObjectDispatcher.zone = zone;
    }


    public static BlockMap getBlockMap(){
        return bm;
    }

    public void setBlockMap(BlockMap map){
        bm=map;
    }

      public static HashMap<String, PlayableChar> getPnjs() {
        return pnjs;
    }

    public static void setPnj(String nom, PlayableChar pnj) {
        pnjs.put(nom, pnj);
    }

    public static int getIdPartida() {
        return idPartida;
    }

    public static void setIdPartida(int idPartida) {
        ObjectDispatcher.idPartida = idPartida;
    }

    
    
}